<?php

namespace app\index\controller;

// use app\common\controller\Frontend;
use app\index\controller\User;
use think\facade\Config;
use think\facade\Cookie;
use think\facade\Hook;
use think\facade\Session;
use think\facade\Validate;

/**
 * 流量情况
 */
class App extends User
{    

    public function initialize()
    {
        parent::initialize();
        $this->token = session("login_token");       
        if(!$this->token){
            $this->redirect("index/user/login");
        }
    }
    
    /**
     * 流量情况
     */
    public function info(){
        $apps = json_decode($this->apps(),true);
        $this->view->assign('title', __('关键应用'));
        $this->view->assign("info",$apps['data']);
        return $this->view->fetch();
        // switch ($apps['code']) {
        //     case '1':
        //         $this->view->assign("info",$apps['data']);
        //         return $this->view->fetch();
        //         break;
        //     case '401':
        //         session(null);
        //         $this->redirect("index/user/login");
        //         break;
        //     default:
        //         break;
        // }
    }
    public function index(){

    }
    public function apps(){
        $data = array(
            'token' => session('login_token'),
            'terminal' => 'web'
        );
        $data = json_encode($data);
        $url = request()->domain() . '/api/user/apps';
        $curlopt = array(
            CURLOPT_HTTPHEADER=>array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $data ) ),
            CURLOPT_TIMEOUT=>Config::get("site.api_timeout"),
        );
        $res = curl_post($url,$data,$curlopt);
        return $res;
    }
    /***************************************
    {
        "code":1,
        "msg":"",
        "time":1547105575,
        "data":[
            {
                "appid":"baidu",
                "appname":"百度搜索"
            },
            {
                "appid":"http",
                "appname":"WWW"
            }
        ]
    }
    ****************************************/
    public function flow(){
        // $app = $this->apps();
        // $apps = json_decode($app,true);
        $type = $this->request->request('type');
        $appid = $this->request->request('appid');
        $data = array(
            'token'     => session('login_token'),
            'terminal'  => 'web',
            'type'      => $type,   // flow|link|snap
            'appid'     => $appid,
        );
        $data = json_encode($data);
        $url = request()->domain() . '/api/application/data';
        $curlopt = array(
            CURLOPT_HTTPHEADER=>array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $data ) ),
            CURLOPT_TIMEOUT=>Config::get("site.api_timeout"),
        );
        $res = curl_post($url,$data,$curlopt);
        echo $res;die;
    }
}
